const _=require('underscore');
const  letter = function *(){
    let code=97;
    while(code){
        yield String.fromCharCode(code);
        code++;
    }
};
const create=()=>{
    let rootId='root';
    let limit=5;
    let count=0;
    let co=letter();
    const children=(()=>{
        //console.log('count:',count);
        if(count>=limit)return [];
        let sibling=_.random(1,3);
        //console.log('sibling:',sibling);
        let list=_.times(sibling,()=>{
            count++;
            return {id:co.next().value,children:children()};
        });
        return list;
    });

    let tree={
        id:rootId,
        children:children()
    };
    let ids=(()=>{
        let ids={};
        (function getIds(tree){
           for(let key in tree){
               if('id' == key){
                   ids[tree.id]=tree.children.map(c=>{
                       getIds(c);
                       return c.id;
                   });

               }
           }
        })(tree);
        return ids;
    })();

    //console.log('ids:',ids);
    let ret={
        ids,
        rootId,
        tree
    };
    return ret;
};
//create();
module.exports=create;